package com.wuzihao.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wuzihao.reggie.common.CustomException;
import com.wuzihao.reggie.dto.SetmealDto;
import com.wuzihao.reggie.entity.Category;
import com.wuzihao.reggie.entity.Setmeal;
import com.wuzihao.reggie.entity.SetmealDish;
import com.wuzihao.reggie.mapper.SetmealMapper;
import com.wuzihao.reggie.service.CategoryService;
import com.wuzihao.reggie.service.SetmealDishService;
import com.wuzihao.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealService setmealService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的数据
        setmealService.save(setmealDto);
        //保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //前端传来的json数据中没有封装setmeal_id数据,重新封装数据
        List<SetmealDish> collect = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(collect);
    }

    @Override
    public Page pageInfo(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(name!=null,"name",name);
        queryWrapper.orderByDesc("update_time");

        Page<Setmeal> setmealPage = setmealService.page(pageInfo, queryWrapper);
        //属性拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        //封装dtoPage的records属性,获得categoryName
        List<SetmealDto> collect = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(collect);

        return dtoPage;
    }

    @Transactional
    public void removeWithDish(List<Long> ids) {
        //1.删除套餐
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.in("id",ids);
        setmealQueryWrapper.eq("status",1);

        int setmealCount = setmealService.count(setmealQueryWrapper);
        if(setmealCount > 0){
            //当前套餐在售，无法删除
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        setmealService.remove(setmealQueryWrapper);
        //2.删除套餐关联的所有菜品
        setmealDishService.remove(new QueryWrapper<SetmealDish>().in("setmeal_id",ids));
    }

    @Override
    public List<Setmeal> listSetmeal(Setmeal setmeal) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,"category_id",setmeal.getCategoryId());
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("update_time");

        List<Setmeal> list = setmealService.list(queryWrapper);
        return list;
    }
}
